package com.test09_抽象类和接口;

import org.junit.Test;

/**
 * 模板设计模式
 */
public class YootkDemo_02 {

    @Test
    public void test1() {
        {
            // 图书的行为
            Action action = new Book2();
            action.command(Action.READ);
            action.command(Action.PAINT);   // 没有反应
        }
        {
            // 画板的行为
            Action action = new Sketchpad2();
            action.command(Action.READ);
            action.command(Action.PAINT);
        }
        {
            // 汽车的行为
            Action action = new Car2();
            action.command(Action.READ);    // 没有反应
            action.command(Action.RUN);
        }
    }
}

/**
 * 描述一个行为
 */
abstract class Action {
    public static final int READ = 1;
    public static final int PAINT = 5;
    public static final int RUN = 10;

    public void command(int code) {
        switch (code) {
            case READ:
                this.read();
                break;
            case PAINT:
                this.paint();
                break;
            case RUN:
                this.run();
                break;
        }
    }

    public abstract void read();
    public abstract void paint();
    public abstract void run();
}

/**
 * 模板设计模式
 * 进行抽象方法的"假"实现
 */
abstract class ActionAdapter extends Action {

    @Override
    public void read() {
    }

    @Override
    public void paint() {
    }

    @Override
    public void run() {
    }
}

/**
 * 图书类
 */
class Book2 extends ActionAdapter {

    @Override
    public void read() {
        System.out.println("【Book】认真读书");
    }
}

/**
 * 画板类
 */
class Sketchpad2 extends ActionAdapter {

    @Override
    public void read() {
        System.out.println("【Sketchpad】读取图像的思想");
    }

    @Override
    public void paint() {
        System.out.println("【Sketchpad】在画板上进行绘图");
    }
}

/**
 * 汽车类
 */
class Car2 extends ActionAdapter {

    @Override
    public void run() {
        System.out.println("【Car】汽车在奔跑");
    }
}
